﻿package mortal.game.scene3D.layer3D.utils
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import com.gengine.global.*;
    import flash.display.*;
    import flash.geom.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.scene3D.player.item.*;
    import mortal.game.utils.*;
    import mortal.mvc.core.*;

    public class ThingUtil extends Object
    {
        public static var isEntitySort:Boolean = false;
        private static var _isNameChange:Boolean = false;
        private static var _isMoveChange:Boolean = false;
        public static var isHeadContainerChange:Boolean = false;
        public static var isRoleGroupChange:Boolean = false;
        public static var isRoleGuildChange:Boolean = false;
        public static var entityUtil:EntityUtil;
        public static var shareCollectEntityUtil:ShareCollectEntityUtil;
        public static var guildCollectTreeUtil:GuildCollectTreeUtil;
        public static var fightUtil:FightEffectUtil;
        public static var npcUtil:NPCUtil;
        public static var effectUtil:EffectUtil;
        public static var effectSound:EffectSoundUtil;
        public static var passUtil:PassUtil;
        public static var jumpUtil:JumpUtil;
        public static var dropItemUtil:DropItemUtil;
        public static var statueUtil:StatueUtil;
        public static var pixsMaskUtil:PixsMaskUtil;
        public static var embedSceneEntityUtil:EmbedSceneEntityUtil;
        private static var _selectEntity:IEntity;
        public static var overEntity:Game2DPlayer;

        public function ThingUtil()
        {
            return;
        }// end function

        public static function get isNameChange() : Boolean
        {
            return _isNameChange;
        }// end function

        public static function set isNameChange(param1:Boolean) : void
        {
            _isNameChange = param1;
            if (param1)
            {
                ThingUtil.isEntitySort = true;
            }
            return;
        }// end function

        public static function get isMoveChange() : Boolean
        {
            return _isMoveChange;
        }// end function

        public static function set isMoveChange(param1:Boolean) : void
        {
            _isMoveChange = param1;
            if (param1)
            {
                ThingUtil.isEntitySort = true;
            }
            return;
        }// end function

        public static function iniThing(param1:PlayerLayer) : void
        {
            entityUtil = new EntityUtil(param1);
            fightUtil = new FightEffectUtil();
            effectUtil = new EffectUtil(param1);
            effectSound = new EffectSoundUtil(param1);
            npcUtil = new NPCUtil(param1);
            passUtil = new PassUtil(param1);
            jumpUtil = new JumpUtil(param1);
            dropItemUtil = new DropItemUtil(param1);
            pixsMaskUtil = new PixsMaskUtil(param1);
            statueUtil = new StatueUtil(param1);
            embedSceneEntityUtil = new EmbedSceneEntityUtil();
            shareCollectEntityUtil = new ShareCollectEntityUtil();
            guildCollectTreeUtil = new GuildCollectTreeUtil();
            return;
        }// end function

        public static function removeAll() : void
        {
            entityUtil.removeAll();
            effectUtil.removeAll();
            effectSound.removeAll();
            npcUtil.removeAll();
            passUtil.removeAll();
            jumpUtil.removeAll();
            dropItemUtil.removeAll();
            pixsMaskUtil.removeAll();
            embedSceneEntityUtil.removeAll();
            shareCollectEntityUtil.removeAll();
            guildCollectTreeUtil.removeAll();
            statueUtil.removeAll();
            return;
        }// end function

        public static function get selectEntity() : IEntity
        {
            return _selectEntity;
        }// end function

        public static function get sceneEntityList() : Vector.<Pivot3D>
        {
            return Game.scene.playerLayer.children;
        }// end function

        public static function onMouseOver(param1:Number, param2:Number) : void
        {
            var _loc_5:EntityInfo = null;
            var _loc_6:Boolean = false;
            var _loc_7:MonsterPlayer = null;
            var _loc_8:BossInfo = null;
            var _loc_3:* = Global.stage.getObjectsUnderPoint(new Point(Global.stage.mouseX, Global.stage.mouseY));
            if (!CursorManager.isMouseInStage || !Game.scene.isInitialize || Rect3DManager.instance.isRectContains(Global.stage.mouseX, Global.stage.mouseY) || _loc_3.length > 0 && !(_loc_3.length == 1 && (_loc_3[0] as DisplayObject).parent is CursorManagerImpl))
            {
                if (ThingUtil.overEntity)
                {
                    ThingUtil.overEntity.onMouseOut();
                    ThingUtil.overEntity = null;
                }
                CursorManager.hideFromScene();
                return;
            }
            if (ThingUtil.overEntity)
            {
                if (ThingUtil.overEntity.hoverTest(SceneRange.display.x, SceneRange.display.y, param1, param2))
                {
                    return;
                }
                ThingUtil.overEntity.onMouseOut();
            }
            var _loc_4:* = getOverObject(param1, param2);
            ThingUtil.overEntity = _loc_4;
            if (_loc_4)
            {
                if (_loc_4)
                {
                    _loc_4.onMouseOver();
                }
                if (_loc_4 is ItemPlayer)
                {
                    CursorManager.showCursor(CursorManager.DROP);
                }
                else if (_loc_4 is NPCPlayer)
                {
                    CursorManager.showCursor(CursorManager.NPCTalk);
                }
                else if (_loc_4 is SpritePlayer)
                {
                    _loc_5 = (_loc_4 as SpritePlayer).entityInfo;
                    _loc_6 = _loc_4 is MonsterPlayer;
                    if (_loc_6)
                    {
                        _loc_7 = _loc_4 as MonsterPlayer;
                    }
                    if (_loc_7 && _loc_7.bossInfo.type == EBossType._EBossTypeCollect)
                    {
                        _loc_8 = _loc_7.bossInfo;
                        if (_loc_8.physDefense == 1)
                        {
                            CursorManager.showCursor(CursorManager.COLLETSAVE);
                        }
                        else
                        {
                            CursorManager.showCursor(CursorManager.COLLET);
                        }
                    }
                    else if (_loc_7 && _loc_7.bossInfo.type == EBossType._EBossTypeTree)
                    {
                        if (_loc_7.entityInfo.entityInfo.life == 0)
                        {
                            CursorManager.showCursor(CursorManager.UNPICK);
                        }
                        else
                        {
                            CursorManager.showCursor(CursorManager.PICK);
                        }
                    }
                    else if (_loc_7 && _loc_7.bossInfo.type == EBossType._EBossTypeSapling)
                    {
                        CursorManager.showCursor(CursorManager.UNPICK);
                    }
                    else if (EntityRelationUtil.isFriend(_loc_5.entityInfo, false))
                    {
                        CursorManager.hideFromScene();
                    }
                    else
                    {
                        CursorManager.showCursor(CursorManager.FIGHT);
                    }
                }
                else
                {
                    CursorManager.hideFromScene();
                }
            }
            else
            {
                CursorManager.hideFromScene();
            }
            return;
        }// end function

        private static function getOverObject(param1:Number, param2:Number) : Game2DPlayer
        {
            var _loc_5:Game2DPlayer = null;
            var _loc_6:Game2DPlayer = null;
            var _loc_8:Pivot3D = null;
            var _loc_3:* = SceneRange.display.x;
            var _loc_4:* = SceneRange.display.y;
            var _loc_7:* = ThingUtil.sceneEntityList;
            for each (_loc_8 in _loc_7)
            {
                
                if (_loc_8 is Game2DPlayer)
                {
                    _loc_5 = _loc_8 as Game2DPlayer;
                    if (_loc_5.hoverTest(_loc_3, _loc_4, param1, param2))
                    {
                        if (!_loc_6)
                        {
                            _loc_6 = _loc_5;
                            continue;
                        }
                        if (_loc_5.overPriority > _loc_6.overPriority)
                        {
                            _loc_6 = _loc_5;
                            continue;
                        }
                        if (_loc_5.overPriority == _loc_6.overPriority && _loc_5.y2d > _loc_6.y2d)
                        {
                            _loc_6 = _loc_5;
                        }
                    }
                }
            }
            return _loc_6;
        }// end function

        public static function set selectEntity(param1:IEntity) : void
        {
            if (_selectEntity == param1 || param1 == RolePlayer.instance || param1 is PetPlayer)
            {
                return;
            }
            if (_selectEntity)
            {
                _selectEntity.selected = false;
            }
            _selectEntity = param1;
            if (_selectEntity)
            {
                _selectEntity.selected = true;
            }
            ThingUtil.isEntitySort = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SelectEntityChange));
            return;
        }// end function

        public static function isNPC(param1:String) : Boolean
        {
            return param1.indexOf(NPCUtil.NPCPlayerLink) == 0;
        }// end function

        public static function isPass(param1:String) : Boolean
        {
            return false;
        }// end function

        public static function removeEntity() : void
        {
            return;
        }// end function

    }
}
